<template>
    <div class="survey">
        <el-form v-loading="loading">
            <el-row class="row" v-for="(item,index) in form.surveyList" :key="item.id">
                <span class="text">是否必答：</span>
                <el-switch v-model="item.mustAnswer"></el-switch>
                <span class="text">标题：</span>
                <el-input v-model="item.questionnaireName"></el-input>
                <el-button class="add-button" v-if="index+1===form.surveyList.length"
                           type="primary" icon="el-icon-plus" @click="add"></el-button>
                <el-button class="remove-button" type="danger" icon="el-icon-minus" @click="remove(index)"></el-button>
            </el-row>
            <el-row class="operation">
                <el-button type="danger" @click="init">取消</el-button>
                <el-button type="primary" @click="save">保存</el-button>
            </el-row>
        </el-form>
    </div>
</template>
<script>

    export default {
        name: 'edit',
        props: ['json'],
        data() {
            return {
                form: {
                    surveyList: [
                        {
                            mustAnswer: false,
                            questionnaireName: ''
                        }
                    ]
                },
                loading: false
            }
        },
        mounted() {
            this.init();
        },
        methods: {
            init() {
                let _this = this;
                _this.loading = true;
                _this.axios({
                    method: 'get',
                    url: '/activityConsole/activityMessage/getQuestionnaireMessage?activityId=' + _this.json.activityId
                }).then(res => {
                    console.log(res);
                    if (res.data.data.length > 0) {
                        _this.form.surveyList = [];
                        for (let item of res.data.data) {
                            _this.form.surveyList.push({
                                mustAnswer: item.mustAnswer === 1,
                                questionnaireName: item.questionnaireName
                            });
                        }
                    }
                    _this.loading = false;
                }).catch(e => {
                    console.warn(e);
                    _this.loading = false;
                });
            },
            add() {
                this.form.surveyList.push({
                    mustAnswer: false,
                    questionnaireName: '',
                });
            },
            remove(index) {
                this.form.surveyList.splice(index,1);
            },
            save() {
                let _this = this;
                _this.loading = true;
                let list = [];
                try {
                    for (let item of _this.form.surveyList) {
                        let obj = Object.assign({
                            isRandom: 0,
                            optionMessageList: [],
                            isDel: 0,
                            isSingle: 0,
                            save_message: true,
                            show_message: false
                        }, item);
                        obj.mustAnswer = obj.mustAnswer ? 1 : 0;
                        list.push(obj);
                    }
                } catch (e) {
                    _this.$message.success('保存时失败');
                    _this.loading = false;
                    return;
                }
                _this.axios({
                    method: 'post',
                    url: '/activityConsole/activityMessage/addQuestionnaireMessage/' + _this.json.activityId,
                    data: list
                }).then(res => {
                    if (res.data.status === '200') {
                        _this.$message.success('保存成功');
                    } else {
                        console.log(res);
                        _this.$message.success('保存失败，' + res.data.description);
                    }
                    _this.loading = false;
                }).catch(e => {
                    _this.$message.success('保存失败');
                    console.warn(e);
                    _this.loading = false;
                });
            }
        },
    }
</script>
<style lang="scss" scoped>
    .survey {
        .el-form {
            margin-top: 30px;
            .row {
                margin-bottom: 20px;
                margin-left: 60px;
                .text {
                    font-size: 15px;
                    margin-left: 10px;
                }
                .el-input {
                    width: 500px;
                }
                .add-button, .remove-button {
                    padding: 8px 8px;
                }
            }
        }
        .operation {
            margin-left: 252px;
            margin-top: 30px;
            .el-button {
                margin-right: 20px;
                padding: 12px 20px;
            }
        }
    }
</style>
